package com.uvlab.sitespot.init;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * Here WebMvcConfigurationSupport provides a way to more finely tune the MVC tier
 * configuration of a Spring based application. In the configureMessageConverters
 * method, the custom converter is being registered and then an explicit call is
 * being made to ensure that the defaults are registered also. A little more work
 * than for a Spring-Boot based application.
 * <p>
 * Customizing HttpMessageConverters with Spring Boot and Spring MVC, 2014,
 * http://www.tuicool.com/articles/jEfiei,
 * http://www.tuicool.com/articles/hit/jEfiei
 *
 * Spring Boot 静态资源处理, 2016,
 * http://blog.csdn.net/catoop/article/details/50501706
 *
 * <p>
 * Created by allen on 6/8/2016.
 */
@Configuration
public class JxWebConfiguration extends WebMvcConfigurationSupport {

    @Bean
    public JxUTF8StringHttpMessageConverter customUtf8StringHttpMessageConverter()
    {
        return new JxUTF8StringHttpMessageConverter();
    }

    // The following is only a demonstration.
    /*
    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }
    */

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //converters.add(customJackson2HttpMessageConverter());
        converters.add(customUtf8StringHttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }
}
